﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ArchitectingInnovation.PRISMvvM.Core.BootstrapperExtensions;
using System.Reflection;
using ArchitectingInnovation.PRISMvvM.Core.Container;

namespace ArchitectingInnovation.PRISMvvM.Core
{
    public class DynamicBootstrapper
    {
        private readonly IContainerWrapper _containerWrapper;
        private readonly IList<IBootstrapConvention> _conventions;
        public DynamicBootstrapper(IContainerWrapper containerWrapper)
        {
            _conventions = new List<IBootstrapConvention>();
            _containerWrapper = containerWrapper;
        }

        public IList<IBootstrapConvention> Conventions
        {
            get { return _conventions; }
        }

        public void Bootstrap(Assembly asm)
        {
            foreach (var type in asm.GetTypes())
            {
                foreach (var type2 in asm.GetTypes())
                {
                    if (type2 == type) continue;
                    foreach (var convention in _conventions)
                    {
                        if(convention.IsMatch(type,type2))
                        {
                            Strap(type, type2);
                        }
                    }
                }
            }
        }

        private void Strap(Type type, Type type2)
        {
            _containerWrapper.Register(type,type2);
        }
    }
}
